/**
 * 调用百度地图
 */
var myValue,mdtids_saveAddress = "",updateregionid = "",map,regiontype,radiusNumber,centerLng,centerLat,pointRectangle,markerStrom = ""; //Map实例、画的图形类型、新增安全区域的半径、新增安全区域的左边、经度、纬度、矩形的4个坐标
function initialize() {
    //创建地图实例
    map = new BMap.Map("map");
    //第1步：设置地图中心点，
    var point = new BMap.Point(104.073516,30.549619);
    map.centerAndZoom(point, 17);
    //第2步：显示地图
    //第3步：启用滚轮放大缩小
    map.enableScrollWheelZoom(true);
    //第4步：向地图中添加缩略图控件
    var top_right_navigation = new BMap.NavigationControl({
        anchor: BMAP_ANCHOR_TOP_LEFT,
        type: BMAP_NAVIGATION_CONTROL_SMALL
    }); //右上角
    map.addControl(top_right_navigation);//向地图添加放大缩小
    // 第5步：向地图中添加比例尺控件
    var top_left_control = new BMap.ScaleControl({
        anchor: BMAP_ANCHOR_BOTTOM_LEFT
    });
    map.addControl(top_left_control);//向地图添加控件
    //地图加载完后
    map.addEventListener("tilesloaded",function(){
        var child = $("#map").children();
        $(child[5]).css("left","5px");
        $(child[6]).hide();
    });
    if(localStorage.followPointInfo == undefined){
        //出事话的时候查询所有的在线设备、根据搜索的类容查询在线设备
        var queryType = $(".school-or-table").html(),queryKey = "schoolname";
        if(queryType == "设备IMEI号"){
            queryKey = "mdtid";
        }else if(queryType == "设备手机号"){
            queryKey = "simid";
        }
        var param="param={";
        param = getJsonDataNotEmpty(param,""+queryKey+"",$(".queryVal").val());
        param += "}";
        var url = serverUrl+'/monitorManager/queryMapMonitorList.json';
        if($(".queryVal").val() != ""){
            url = url +"?"+param;
        }
        suixun.ajax(url,'',"get",function(data){
            setTimeout(function () {
                suixun.hideLoding(500);//隐藏加载中
            },1500);
            if(data.code == 200 && data.info != null){
                $(".BMap_scaleCtrl").addClass("BMap_scaleCtrlShow");//显示游标尺
                if(data.info.length == 0){
                    suixun.alert("没有学生数据!");
                }else{
                    point = new BMap.Point((parseFloat(data.info[0].lon)+parseFloat(data.info[0].lonx))/1000000,(parseFloat(data.info[0].lat)+parseFloat(data.info[0].laty))/1000000);
                    //设置中心点坐标和地图级别。-地图初始化
                    map.centerAndZoom(point, 17);
                    for (var i = 0; i < data.info.length; i++) {
                        var lon = (parseFloat(data.info[i].lon)+parseFloat(data.info[i].lonx))/1000000;
                        var lat = (parseFloat(data.info[i].lat)+parseFloat(data.info[i].laty))/1000000;
                        if(lon != 0 && lat != 0){
                            var maker = addMarker(new window.BMap.Point(lon, lat), i);
                            //位置 ，经度，纬度，最后上线时间,alarmtype(报警的值为20 代表电量)，电量的值为1,2,3,4，以25%为基准比例）,学生id
                            addInfoWindow(maker, data.info[i].objectid,lon,lat,data.info[i].maxrecvtime,data.info[i].alarmtype,data.info[i].alarmvalue);
                        }
                    }
                }
            }else{
                suixun.alert("没有学生数据!");
            }
        });
    }else{
        var param="param={";
        param = getJsonDataNotEmpty(param,"objectid",""+localStorage.followPointInfo.split("*")[0]+"");
        param = getJsonDataNotEmpty(param,"maxrecvtime","'"+localStorage.followPointInfo.split("*")[1]+"'");
        param += "}";
        suixun.ajax(serverUrl+'/monitorManager/queryMapMonitorsByFollow.json',param,"get",function(data){
            setTimeout(function () {
                suixun.hideLoding(500);//隐藏加载中
            },500);
            if(data.code == 200 && data.info != null){
                $(".BMap_scaleCtrl").addClass("BMap_scaleCtrlShow");//显示游标尺
                if(data.info.length == 0){
                    suixun.alert("没有找到学生数据!");
                }else{
                    //第7步：绘制点和线 1 2 3 4
                    point = new BMap.Point((parseFloat(data.info[0].lon)+parseFloat(data.info[0].lonx))/1000000, (parseFloat(data.info[0].lat)+parseFloat(data.info[0].laty))/1000000);
                    //第2步：初始化地图,设置中心点坐标和地图级别。-地图初始化
                    map.centerAndZoom(point, 17);
                    if(data.info.length == 1){
                        var obj = data.info[0],
                            p_lng = (parseFloat(obj.lon)+parseFloat(obj.lonx))/1000000,
                            p_lat = (parseFloat(obj.lat)+parseFloat(obj.laty))/1000000;
                        if(p_lng != 0 && p_lat != 0){
                            var maker = addMarkerLine(new window.BMap.Point(p_lng, p_lat), 0);//绘制头像覆盖物
                            addInfoWindowLin(maker,p_lng,p_lat,obj.recvtime);
                        }
                    }else{
                        for(var i = 0; i < data.info.length; i++) {
                            var obj = data.info[i],
                                p_lng = (parseFloat(obj.lon)+parseFloat(obj.lonx))/1000000,
                                p_lat = (parseFloat(obj.lat)+parseFloat(obj.laty))/1000000;
                            if(p_lng != 0 && p_lat != 0){
                                var maker = addMarkerLine(new window.BMap.Point(p_lng, p_lat), i);//绘制头像覆盖物
                                addInfoWindowLin(maker,p_lng,p_lat,obj.recvtime);
                            }
                            if(i==0){//第一个点
                                var p_next_lng = (parseFloat(data.info[i+1].lon)+parseFloat(data.info[i+1].lonx))/1000000;
                                var p_next_lat = (parseFloat(data.info[i+1].lat)+parseFloat(data.info[i+1].laty))/1000000;
                                line(p_lng,p_lat,p_next_lng,p_next_lat);//创建线
                            }else if(i == data.info.length-1){//最后一个点
                                localStorage.removeItem("followPointInfo");//移除选择跟踪的学生
                            }else{//中间的一个点
                                var p_next_lng = (parseFloat(data.info[i+1].lon)+parseFloat(data.info[i+1].lonx))/1000000;
                                var p_next_lat = (parseFloat(data.info[i+1].lat)+parseFloat(data.info[i+1].laty))/1000000;
                                line(p_lng,p_lat,p_next_lng,p_next_lat);//创建线
                            }

                        }
                    }
                }
            }else{
                suixun.alert("追踪失败!",function (data) {
                    if(data){
                        localStorage.removeItem("followPointInfo");
                        var script = document.createElement("script");
                        script.src = "http://api.map.baidu.com/api?v=2.0&ak=csjf0Mv4PUoe61rIrQzd1dgKHhzXr3Vv&callback=initialize";//此为v2.0版本的引用方式
                        document.body.appendChild(script);
                    }
                });
            }
        });
    }
    //添加地址的时候所用到的方法
    function G(id) {
        return document.getElementById(id);
    }
    var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
        {"input" : "suggestId"
            ,"location" : map
        });
    ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
        var str = "";
        var _value = e.fromitem.value;
        var value = "";
        if (e.fromitem.index > -1) {
            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        }
        str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
        value = "";
        if (e.toitem.index > -1) {
            _value = e.toitem.value;
            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        }
        str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
        G("searchResultPanel").innerHTML = str;
    });

    ac.addEventListener("onconfirm", function(e) { //鼠标点击下拉列表后的事件
        var _value = e.item.value;
        myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        //G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
        setPlace();
    });
}

// 添加信息窗口
function addInfoWindow(marker, objectid,lon,lat,maxrecvtime,alarmtype,alarmvalue) {
    var title = "", html = [];
    //需要先渲染一次弹出框的dom，在第一次点击的时候才进行修改dom样式
    var openInfoWinFun = function () {
        if(maxrecvtime == null){
            maxrecvtime = "";
        }
        if(alarmtype == "20"){
            if(alarmvalue == "4" ){
                alarmvalueImg = "electricity-four.png";
            }else if(alarmvalue == "3" ){
                alarmvalueImg = "electricity-three.png";
            }else if(alarmvalue == "2" ){
                alarmvalueImg = "electricity-two.png";
            }else if(alarmvalue == "1" ){
                alarmvalueImg = "electricity-one.png";
            }else{
                alarmvalueImg = "electricity-zero.png";
            }
            title = '<div class="map-pop-title">' +
                        '<img src="../img/icon/'+alarmvalueImg+'" />' +
                        '<span>'+maxrecvtime+'</span>' +
                    '</div>';
        }else{
            title = '<div class="map-pop-title" style="font-size:14px">' +
                '<span style="margin-left: 5px">'+maxrecvtime+'</span>' +
                '</div>';
        }
        var infoWindow = new BMap.InfoWindow(html.join(""), {title: title, width: 350, height: 70});//title: title,
        var pointAddre = "";
        var pointAddress = new BMap.Point(lon,lat);
        var gc = new BMap.Geocoder();
        gc.getLocation(pointAddress, function(rs){
            var addComp = rs.addressComponents;
            pointAddre = addComp.province +addComp.city + addComp.district +addComp.street + addComp.streetNumber;
            html = [];
            html.push('<div class="map-pop-info">' +
                '<div class="map-pop-info-address" title="'+pointAddre+'">'+pointAddre+'</div>' +
                '<div class="map-pop-info-onLine" style="display: none">' +
                '<img src="../img/icon/time-blue.png"/>' +
                '<span>在线时长：</span>' +
                '</div>' +
                '<div class="map-pop-info-button">' +
                '<span class="track" onclick="track('+objectid+')">跟踪</span>' +
                '<span class="directive" style="display: none" onclick="directive()">指令</span>' +
                '</div>' +
                '</div>');
            infoWindow = new BMap.InfoWindow(html.join(""), {title: title, width: 350, height: 70});//title: title,
            marker.openInfoWindow(infoWindow);
            //修改弹出层样式
            var child = $(".BMap_pop").children();
            $($(child[0]).children()[0]).addClass(" BMap-left-top");//左上角
            $($(child[2]).children()[0]).addClass(" BMap-right-top");//右上角
            $($(child[4]).children()[0]).addClass(" BMap-left-bottom");//左下角
            $($(child[6]).children()[0]).addClass(" BMap-right-bottom");//右下角
            $($(child[7]).children()[0]).hide();//三角形
            $(child[7]).append("<span class='BMap-bottom-triangle'></span>");//隐藏原始图标
            $(".BMap_pop").css("marginLeft",0+"px");
            $(".BMap_pop").css("marginTop",0+"px");
        });
    };
    marker.addEventListener("click", openInfoWinFun);
    return openInfoWinFun;
}

// 添加标注-所有学生
function addMarker(point, index) {
    var myIcon = new BMap.Icon("../img/icon/child.png",
        new BMap.Size(64, 64), {
            anchor: new BMap.Size(19, 8),// 图标中央下端的尖角位置。
        });
    var marker = new BMap.Marker(point, { icon: myIcon });
    map.addOverlay(marker);
    return marker;
}

// 添加标注-添加线的起止点
function addMarkerLine(point, index) {
    var myIcon = new BMap.Icon("../img/icon/child.png",
        new BMap.Size(64, 64), {
            anchor: new BMap.Size(32, 64),// 图标中央下端的尖角位置。
        });
    var marker = new BMap.Marker(point, { icon: myIcon });
    map.addOverlay(marker);
    return marker;
}
//设置线的覆盖物
function line(p_lng,p_lat,p_next_lng,p_next_lat) {
    var polyline = new BMap.Polyline([
        new BMap.Point(p_lng, p_lat),
        new BMap.Point(p_next_lng, p_next_lat)
    ], {
        strokeColor:"#01b4ee",//设置颜色
        strokeWeight:4,//宽度
        strokeOpacity:0.5//透明度
    });
    map.addOverlay(polyline);//添加到地图上面去
}
// 添加信息窗口-追踪
function addInfoWindowLin(marker,lon,lat,recvtime) {
    var title, html = [];
    //需要先渲染一次弹出框的dom，在第一次点击的时候才进行修改dom样式
    var openInfoWinFun = function () {
        var recvtimeIsNone = true;
        if(recvtime != null) {
            title = '<div class="map-pop-title" style="font-size:14px">' +
                '<span style="margin-left: -8px">'+recvtime+'</span>' +
                '</div>';
            recvtimeIsNone = false;
        }else{
            title = '';
        }
        var pointAddress = new BMap.Point(lon,lat);
        var gc = new BMap.Geocoder();
        gc.getLocation(pointAddress, function(rs){
            var addComp = rs.addressComponents;
            var pointAddre = addComp.province +addComp.city + addComp.district +addComp.street + addComp.streetNumber;
            html = [];
            if(pointAddre ==null){
                pointAddre = "没有获取到地址信息"
            }
            if(recvtimeIsNone){
                html.push('<div class="map-pop-info">' +
                    '<div class="map-pop-info-address" title="'+pointAddre+'">'+pointAddre+'</div>' +
                    '<div class="map-pop-info-button cancer-zuizhong">' +
                    '<span class="track" onclick="removeTrack()">取消跟踪</span>' +
                    '</div>' +
                    '</div>');
            }else{
                html.push('<div class="map-pop-info">' +
                    '<div class="map-pop-info-address" title="'+pointAddre+'">'+pointAddre+'</div>' +
                    '<div class="map-pop-info-button cancer-zuizhong">' +
                    '<span class="track" style="top: 0;" onclick="removeTrack()">取消跟踪</span>' +
                    '</div>' +
                    '</div>');
            }
            infoWindow = new BMap.InfoWindow(html.join(""), {title:title,width: 350, height: 20});//title: title,
            marker.openInfoWindow(infoWindow);
            //修改弹出层样式
            var child = $(".BMap_pop").children();
            $($(child[0]).children()[0]).addClass(" BMap-left-top");//左上角
            $($(child[2]).children()[0]).addClass(" BMap-right-top");//右上角
            $($(child[4]).children()[0]).addClass(" BMap-left-bottom");//左下角
            $($(child[6]).children()[0]).addClass(" BMap-right-bottom");//右下角
            $($(child[7]).children()[0]).hide();//三角形
            $(child[7]).append("<span class='BMap-bottom-triangle'></span>");//隐藏原始图标
            $(".BMap_pop").css("marginLeft",-13+"px");
            $(".BMap_pop").css("marginTop",-56+"px");
        });
    };
    marker.addEventListener("click", openInfoWinFun);
    return openInfoWinFun;
}

//更具地址设置点
var overlays = "";//所画的图形
function setPlace(){
    map.clearOverlays();//清除地图上所有覆盖物
    if(updateregionid == ""){
        $(".saveMap").show();//保存按钮
        $(".addMap").show();//新增按钮
    }else{
        $(".saveMap").hide();//隐藏保存
        $(".addMap").hide();//新增按钮
        $(".updateMap").show();//修改按钮
    }
    $(".popAddMessage").hide();//搜索地址关闭弹出层
    $(".removeMap").show();//移除按钮
    $(".reset").show();//重置按钮

    function myFun(){
        var pp = local.getResults().getPoi(0).point;//获取第一个智能搜索的结果
        map.centerAndZoom(pp, 18);
        map.addOverlay(new BMap.Marker(pp));//添加标注-可以自定义图片
    }
    var local = new BMap.LocalSearch(map, { //智能搜索
        onSearchComplete: myFun
    });
    //新增安全区域
    //获取值
    var overlaycomplete = function(e){
        removeLast();//移除上一个画的圆
        overlays = e.overlay;
        //如果是圆就把结果拿出来
        if (e.drawingMode == BMAP_DRAWING_CIRCLE) {
            radiusNumber = e.overlay.getRadius();centerLng = e.overlay.getCenter().lng;centerLat = e.overlay.getCenter().lat;
            if(radiusNumber > 1000 || radiusNumber < 100){
                suixun.alert("安全范围：100至1000米<br>设置范围："+radiusNumber.toFixed(2)+"米",function (data) {
                    if(data == true){
                        removeLast();//移除上一个画的圆
                        overlays = "";
                    }
                });
            }else{
                regiontype = "1";//1：圆形
            }
        }else if (e.drawingMode == BMAP_DRAWING_RECTANGLE){
            pointRectangle = e.overlay.Ku;//
            radiusNumber = map.getDistance(new BMap.Point(pointRectangle.Ee,pointRectangle.Ke),new BMap.Point(pointRectangle.Je,pointRectangle.Fe)).toFixed(2);
            if(radiusNumber > 1000 || radiusNumber < 100){
                suixun.alert("安全范围：100至1000米<br>设置范围："+radiusNumber+"米",function (data) {
                    if(data == true){
                        removeLast();//移除上一个画的圆
                        overlays = "";
                    }
                });
            }else{
                regiontype = "0";//0：矩形
            }
        }
    };
    //绘制图形的样式
    var styleOptions = {
        strokeColor:"#B8C7E4",    //边线颜色。
        fillColor:"#B8C7E4",      //填充颜色。当参数为空时，圆形将没有填充效果。
        strokeWeight: 3,       //边线的宽度，以像素为单位。
        strokeOpacity: 0.8,	   //边线透明度，取值范围0 - 1。
        fillOpacity: 0.6,      //填充的透明度，取值范围0 - 1。
        strokeStyle: 'solid' //边线的样式，solid或dashed。
    };
    /**
     * 点 、 线、多变形 在样式里面设置了display:none 的
     * BMapLib_marker_hover、BMapLib_polyline、BMapLib_polygon、BMapLib_marker
     * @type {BMapLib.DrawingManager}
     */
    var drawingManager = new BMapLib.DrawingManager(map, {
        isOpen: false, //是否开启绘制模式
        enableDrawingTool: true,//是否添加绘制工具栏控件，默认不添加
        enableCalculate: false,//绘制是否进行测距(画线时候)、测面(画圆、多边形、矩形)
        drawingToolOptions: {
            anchor: BMAP_ANCHOR_TOP_RIGHT, //位置
            offset: new BMap.Size(5, 5), //偏离值
            /**
             * BMAP_DRAWING_MARKER,//点
             * BMAP_DRAWING_POLYLINE,//线
             * BMAP_DRAWING_POLYGON,//多边行
             * BMAP_DRAWING_RECTANGLE//矩形
             *  这个无效，是通过css样式使用display:none隐藏的
             */
            drawingTypes : [
                BMAP_DRAWING_CIRCLE
            ]
        },

        circleOptions: styleOptions, //圆的样式
//                    polylineOptions: styleOptions, //线的样式
//                    polygonOptions: styleOptions, //多边形的样式
//         rectangleOptions: styleOptions //矩形的样式
    });
    /*
    * 文档地址
    * http://api.map.baidu.com/library/DrawingManager/1.4/docs/symbols/BMapLib.DrawingManager.html
    * 绘制完成后触发的事件 绘制不同的图形对应不同的点
    * */
    drawingManager.addEventListener('overlaycomplete', overlaycomplete);
    local.search(myValue);
}
//跟踪
function track(objectid) {
    var param="param={";
    param = getJsonDataNotEmpty(param,"objectid",""+objectid+"");
    param += "}";
    suixun.ajax(serverUrl+'/monitorManager/queryMapMonitorsByFollow.json',param,"get",function(data){
        if(data.code == 200 && data.info != null){
            //第7步：绘制点和线 1 2 3 4
            map.clearOverlays();//清除地图上所有覆盖物
            for(var i = 0; i < data.info.length; i++) {
                var obj = data.info[i],
                    p_lng = parseFloat(parseFloat(obj.lon)+parseFloat(obj.lonx))/1000000,
                    p_lat = parseFloat(parseFloat(obj.lat)+parseFloat(obj.laty))/1000000;
                if(p_lng != 0 && p_lat != 0){
                    var maker = addMarkerLine(new window.BMap.Point(p_lng, p_lat), i);//绘制头像覆盖物
                    addInfoWindowLin(maker,p_lng,p_lat,obj.recvtime);
                }
                if(i==0){//第一个点
                    var p_next_lng = parseFloat(parseFloat(data.info[i+1].lon)+parseFloat(data.info[i+1].lonx))/1000000;
                    var p_next_lat = parseFloat(parseFloat(data.info[i+1].lat)+parseFloat(data.info[i+1].laty))/1000000;
                    line(p_lng,p_lat,p_next_lng,p_next_lat);//创建线
                }else if(i == data.info.length-1){//最后一个点
                    localStorage.removeItem("followPointInfo");//移除选择跟踪的学生
                }else{//中间的一个点
                    var p_next_lng = parseFloat(parseFloat(data.info[i+1].lon)+parseFloat(data.info[i+1].lonx))/1000000;
                    var p_next_lat = parseFloat(parseFloat(data.info[i+1].lat)+parseFloat(data.info[i+1].laty))/1000000;
                    line(p_lng,p_lat,p_next_lng,p_next_lat);//创建线
                }
            }
        }else{
            suixun.alert("追踪失败!");
        }
    });
}
//取消追踪
function removeTrack() {
    selectMapPoint();
}
//指令
function directive() {
    alert('点击指令');
}
//移除画安全区域的点
function removeMap() {
    $(".BMapLib_Drawing").remove();
    $(".removeMap").hide();
    $(".saveMap").hide();
    $(".reset").hide();
    $(".updateMap").hide();//修改按钮
    $(".addMap").show();//新增按钮
    selectMapPoint();//查询显示到地图上面的学生
    $("#suggestId").val("");
    updateregionid = "";//修改是的id
}
//查询显示到地图上面的学生
function selectMapPoint(){
    suixun.showLoding();//显示加载中
    map.clearOverlays();//清除地图上所有覆盖物
    var queryType = $(".school-or-table").html(),queryKey = "schoolname";
    if(queryType == "设备IMEI号"){
        queryKey = "mdtid";
    }else if(queryType == "设备手机号"){
        queryKey = "simid";
    }
    var param="param={";
    param = getJsonDataNotEmpty(param,""+queryKey+"",$(".queryVal").val());
    param += "}";
    var url = serverUrl+'/monitorManager/queryMapMonitorList.json';
    if($(".queryVal").val() != ""){
        url = url +"?"+param;
    }
    suixun.ajax(url,'',"get",function(data){
        suixun.hideLoding();//隐藏加载中
        if(data.code == 200 && data.info != null){
            if(data.info.length == 0){
                point = new BMap.Point(104.073516,30.549619);
                map.centerAndZoom(point, 17);
                return;
            }
            if(data.info[0].lon != 0 && data.info[0].lat != 0 && data.info[0].lon != null){
                var point = new BMap.Point((parseFloat(data.info[0].lon)+parseFloat(data.info[0].lonx))/1000000,(parseFloat(data.info[0].lat)+parseFloat(data.info[0].laty))/1000000);
                //设置中心点坐标和地图级别。-地图初始化
                map.centerAndZoom(point, 17);
            }
            for (var i = 0; i < data.info.length; i++) {
                var lon = (parseFloat(data.info[i].lon)+parseFloat(data.info[i].lonx))/1000000;
                var lat = (parseFloat(data.info[i].lat)+parseFloat(data.info[i].laty))/1000000;
                if(lon != 0 && lat != 0){
                    var maker = addMarker(new window.BMap.Point(lon, lat), i);
                    //位置 ，经度，纬度，最后上线时间,alarmtype(报警的值为20 代表电量)，电量的值为1,2,3,4，以25%为基准比例）,学生id
                    addInfoWindow(maker, data.info[i].objectid,lon,lat,data.info[i].maxrecvtime,data.info[i].alarmtype,data.info[i].alarmvalue);
                }
            }
        }else{
            suixun.alert("没有学生数据!");
            point = new BMap.Point(104.073516,30.549619);
            map.centerAndZoom(point, 17);
        }
    });
}
//保存安全区域
function saveMap(callback) {
    //regiontype,radiusNumber,centerLng,centerLat,pointRectangle; //画的图形类型、新增安全区域的半径、新增安全区域的左边、经度、纬度、矩形的4个坐标
    var addressName = $("#addressName").val();
    if(addressName == ""){
        suixun.alert("安全区域名称不能为空!",function (data) {
            if(data){
                $(".popAddMessage").show();
            }
        });
        return;
    }
    if($("#searchName").val() == ""){
        suixun.alert("搜索框内容不能为空!");
        return;
    }
    //验证搜索框是否为空
    var queryType = $(".school-or-table").html();
    if(queryType == "学校"){
        var param="param={";
        param = getJsonDataNotEmpty(param,"schoolname",""+$("#searchName").val()+"");
        param = getJsonDataNotEmpty(param,"pagenumber","1");
        param = getJsonDataNotEmpty(param,"pagecount","10000");
        param += "}";
        suixun.ajax(serverUrl+'/studentInfoManage/queryStduentList.json',param,"get",function(data){
            if(data.info != "" && data.info != null){
                mdtids_saveAddress = "";
                for(var i=0;i<data.info.queryresult.length;i++){
                    var obj=data.info.queryresult[i];
                    if(mdtids_saveAddress.indexOf(obj.mdtid) == -1){
                        mdtids_saveAddress +=obj.mdtid+ ",";
                    }
                }
                if(mdtids_saveAddress.split(",").length>0){
                    mdtids_saveAddress = mdtids_saveAddress.substr(0,mdtids_saveAddress.length-1);
                }
                saveAddress(function (data) {
                    callback(data);
                });//调用保存方法
            }else{
                suixun.alert("学校："+$(".imeiOrphone").val()+"未添加设备!");
            }
        });
    }else{
        saveAddress(function (data) {
            callback(data);
        });//调用保存方法
    }
}
//保存安全区域
function  saveAddress(callback) {
    var queryType = $(".school-or-table").html(),queryKey = "mdtids", regionaddr=$("#suggestId").val(),type = 1;
    if(queryType == "设备IMEI号"){
        mdtids_saveAddress = $("#searchName").val();
        queryKey = "mdtids";
        type = 2;
    }else if(queryType == "设备手机号"){
        mdtids_saveAddress = $("#searchName").val();
        queryKey = "simid";
        type = 3;
    }else{
        regionaddr = $("#searchName").val();
    }
    //验证区域
    if(overlays == ""){
        suixun.alert("请在地图上画区域");
        return;
    }
    var param = "";
    if(regiontype == 0){//矩形
        param="param={";
        param = getJsonDataNotEmpty(param,"minlon",'"'+parseInt(pointRectangle.Je*1000000)+'"');
        param = getJsonDataNotEmpty(param,"minlat",'"'+parseInt(pointRectangle.Ke*1000000)+'"');
        param = getJsonDataNotEmpty(param,"maxlon",'"'+parseInt(pointRectangle.Ee*1000000)+'"');
        param = getJsonDataNotEmpty(param,"maxlat",'"'+parseInt(pointRectangle.Fe*1000000)+'"');
        param = getJsonDataNotEmpty(param,"regiontype",'"'+regiontype+'"');
        param = getJsonDataNotEmpty(param,"type",'"'+type+'"');
        param = getJsonDataNotEmpty(param,"regionid",'"'+updateregionid+'"');
        param = getJsonDataNotEmpty(param,"regionname",'"'+$("#addressName").val()+'"');
        param = getJsonDataNotEmpty(param,"regionaddr",'"'+regionaddr+'"');
        param = getJsonDataNotEmpty(param,"radius",'"'+parseFloat(radiusNumber).toFixed(0)+'"');
        param = getJsonDataNotEmpty(param,'"'+queryKey+'"','"'+mdtids_saveAddress+'"');
        param += "}";
    }else{//圆形
        param="param={";
        param = getJsonDataNotEmpty(param,"centerlon",'"'+parseInt(centerLng*1000000)+'"');
        param = getJsonDataNotEmpty(param,"centerlat",'"'+parseInt(centerLat*1000000)+'"');
        param = getJsonDataNotEmpty(param,"regiontype",'"'+regiontype+'"');
        param = getJsonDataNotEmpty(param,"type",'"'+type+'"');
        param = getJsonDataNotEmpty(param,"regionid",'"'+updateregionid+'"');
        param = getJsonDataNotEmpty(param,"regionname",'"'+$("#addressName").val()+'"');
        param = getJsonDataNotEmpty(param,"regionaddr",'"'+regionaddr+'"');
        param = getJsonDataNotEmpty(param,"radius",'"'+parseFloat(radiusNumber).toFixed(0)+'"');
        param = getJsonDataNotEmpty(param,'"'+queryKey+'"','"'+mdtids_saveAddress+'"');
        param += "}";
    }
    if(updateregionid == ""){
        //写入参数
        suixun.ajax(serverUrl+'/monitorManager/addSafeRegion.json',param,"get",function(data){
            if(data.code == 200){
                suixun.alert(data.info.result,function (data) {
                    if(data){
                        callback(true);
                        removeMap();
                    }
                });
            }else{
                callback(false);
                suixun.alert(data.msg);
            }
        });
    }else{
        //写入参数
        suixun.ajax(serverUrl+'/monitorManager/updateSafeRegion.json',param,"get",function(data){
            if(data.code == 200){
                suixun.alert(data.info,function (data) {
                    if(data){
                        callback(true);
                        removeMap();
                    }
                });
            }else{
                callback(false);
                suixun.alert(data.msg);
            }
        });
    }
}
//修改
function updateMap() {
}
//重置
function reset() {
    $(".BMapLib_Drawing").remove();
    setPlace();
}
//移除上一个画的图形
function removeLast() {
    if(overlays != ""){
        map.removeOverlay(overlays);
    }
}
/**
 * 移除地图上指定覆盖物
 * @param marker
 */
function removeOverlay(marker){
    map.removeOverlay(marker);
}
